package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.comprehensive.domain.GasPrevention;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.dto.GasPreventionExamineDto;
import com.ruoyi.comprehensive.mapper.GasPreventionMapper;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.vo.GasPreventionExamineVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.GasPreventionExamineMapper;
import com.ruoyi.comprehensive.domain.GasPreventionExamine;
import com.ruoyi.comprehensive.service.IGasPreventionExamineService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 瓦斯审核Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-14
 */
@Service
public class GasPreventionExamineServiceImpl implements IGasPreventionExamineService 
{
    @Autowired
    private GasPreventionExamineMapper gasPreventionExamineMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    @Autowired
    private GasPreventionMapper gasPreventionMapper;
    /**
     * 查询瓦斯审核
     * 
     * @param id 瓦斯审核主键
     * @return 瓦斯审核
     */
    @Override
    public GasPreventionExamine selectGasPreventionExamineById(String id)
    {
        return gasPreventionExamineMapper.selectGasPreventionExamineById(id);
    }

    /**
     * 查询瓦斯审核列表
     * 
     * @param gasPreventionExamine 瓦斯审核
     * @return 瓦斯审核
     */
    @Override
    public List<GasPreventionExamine> selectGasPreventionExamineList(GasPreventionExamine gasPreventionExamine)
    {
        return gasPreventionExamineMapper.selectGasPreventionExamineList(gasPreventionExamine);
    }

    /**
     * 新增瓦斯审核
     * 
     * @param gasPreventionExamine 瓦斯审核
     * @return 结果
     */
    @Override
    public int insertGasPreventionExamine(GasPreventionExamine gasPreventionExamine)
    {
        gasPreventionExamine.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return gasPreventionExamineMapper.insertGasPreventionExamine(gasPreventionExamine);
    }

    /**
     * 修改瓦斯审核
     * 
     * @param gasPreventionExamine 瓦斯审核
     * @return 结果
     */
    @Override
    public int updateGasPreventionExamine(GasPreventionExamine gasPreventionExamine)
    {
        GasPreventionExamine gasPreventionExamine1 = new GasPreventionExamine();
        BeanUtils.copyProperties(gasPreventionExamine,gasPreventionExamine1);
        gasPreventionExamine1.setUpdateAt(new Date());
        gasPreventionExamine1.setUpdateByName(SecurityUtils.getUsername());
        gasPreventionExamine1.setUpdateBy(SecurityUtils.getUserId().toString());
        GasPrevention gasPrevention = gasPreventionMapper.selectGasPreventionById(gasPreventionExamine.getGasPreventionId());
        if (gasPrevention != null){
            GasPrevention gasPrevention1 = new GasPrevention();
            gasPrevention1.setExamineStatus(gasPreventionExamine.getExamineStatus());
            gasPrevention1.setUpdateAt(new Date());
            gasPrevention1.setUpdateByName(SecurityUtils.getUsername());
            gasPrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
            gasPreventionMapper.updateGasPrevention(gasPrevention1);
        }
        return gasPreventionExamineMapper.updateGasPreventionExamine(gasPreventionExamine1);
    }

    /**
     * 批量删除瓦斯审核
     * 
     * @param ids 需要删除的瓦斯审核主键
     * @return 结果
     */
    @Override
    public int deleteGasPreventionExamineByIds(String[] ids)
    {
        return gasPreventionExamineMapper.deleteGasPreventionExamineByIds(ids);
    }

    /**
     * 删除瓦斯审核信息
     * 
     * @param id 瓦斯审核主键
     * @return 结果
     */
    @Override
    public int deleteGasPreventionExamineById(String id)
    {
        return gasPreventionExamineMapper.deleteGasPreventionExamineById(id);
    }

    /**
     * 获取瓦斯审核详细信息
     * @param id
     * @return
     */
    @Override
    public GasPreventionExamineDto getGasPreventionExamineById(String id) {
        GasPreventionExamineDto gasPreventionExamineDto = new GasPreventionExamineDto();
        // 通过ID获取瓦斯审核信息
        GasPreventionExamine gasPreventionExamine = gasPreventionExamineMapper.selectGasPreventionExamineById(id);
        GasPrevention gasPrevention = gasPreventionMapper.selectGasPreventionById(gasPreventionExamine.getGasPreventionId());
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(gasPreventionExamine.getId());
        preventionEnclosure.setEnclosureType("4");
        //对象赋值
        BeanUtils.copyProperties(gasPreventionExamine,gasPreventionExamineDto);
        // 查询附件信息内容
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        gasPreventionExamineDto.setPreventionEnclosures(preventionEnclosures);
        gasPreventionExamineDto.setGasPreventionDate(gasPrevention.getGasPreventionDate());
        gasPreventionExamineDto.setName(gasPrevention.getName());
        gasPreventionExamineDto.setGasPreventionCode(gasPrevention.getGasPreventionCode());
        return gasPreventionExamineDto;
    }

    /**
     * 查询瓦斯审核列表
     * @param vo
     * @return
     */
    @Override
    public PageInfo<GasPreventionExamineDto> getGasPreventionExamineList(GasPreventionExamineVo vo) {
        // 开始分页
        startPage();
        List<GasPreventionExamine> gasPreventionExamineList = gasPreventionExamineMapper.getGasPreventionExamineList(vo);
        //  获取分页信息
        PageInfo gasExamineDtoInfo = new PageInfo<>(gasPreventionExamineList);
        List<GasPreventionExamineDto> gasPreventionExamineDtoList = gasPreventionExamineList.stream().map(gasPreventionExamine1 -> {
            GasPreventionExamineDto gasPreventionExamineDto = new GasPreventionExamineDto();
            BeanUtils.copyProperties(gasPreventionExamine1, gasPreventionExamineDto);
            GasPrevention gasPrevention = gasPreventionMapper.selectGasPreventionById(gasPreventionExamine1.getGasPreventionId());
            gasPreventionExamineDto.setGasPreventionDate(gasPrevention.getGasPreventionDate());
            gasPreventionExamineDto.setGasPreventionCode(gasPrevention.getGasPreventionCode());
            gasPreventionExamineDto.setName(gasPrevention.getName());
            return gasPreventionExamineDto;
        }).collect(Collectors.toList());
        // 将封装后的列表放到分页对象中
        gasExamineDtoInfo.setList(gasPreventionExamineDtoList);
        return gasExamineDtoInfo;
    }
}
